package com.cskaoyan.service.wx;

import com.cskaoyan.Utils.MyUtils;
import com.cskaoyan.bean.goods.*;
import com.cskaoyan.bean.mallbean.*;
import com.cskaoyan.bean.promotebean.Groupon_rules;
import com.cskaoyan.bean.userbean.Collect;
import com.cskaoyan.bean.userbean.CollectExample;
import com.cskaoyan.bean.userbean.Footprint;
import com.cskaoyan.bean.userbean.FootprintExample;
import com.cskaoyan.mapper.goodsmapper.*;
import com.cskaoyan.mapper.mallmapper.BrandMapper;
import com.cskaoyan.mapper.mallmapper.CategoryMapper;
import com.cskaoyan.mapper.mallmapper.IssueMapper;
import com.cskaoyan.mapper.usermapper.CollectMapper;
import com.cskaoyan.mapper.usermapper.FootprintMapper;
import com.cskaoyan.vo.wx.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.geom.GeneralPath;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author hsp
 * @date 2021/5/13 20:04
 */
@Service
public class WxGoodsServiceImpl implements WxGoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    GoodsCommentMapper goodsCommentMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    IssueMapper issueMapper;
    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;

    @Override
    public Integer getGoodsCount() {

        Long count = goodsMapper.countByExample(new GoodsExample());
        return count.intValue();
    }

    @Override
    public WxCategoryVo getCategoryMessage(Integer id) {
        WxCategoryVo wxCategoryVo = new WxCategoryVo();
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andDeletedEqualTo(0).andIdEqualTo(id);
        List<Category> currentCategorys = categoryMapper.selectByExample(categoryExample);
        Category currentCategory = currentCategorys.get(0);
        Integer pid = currentCategory.getPid();
        CategoryExample categoryExample1 = new CategoryExample();
        categoryExample1.createCriteria().andPidEqualTo(pid).andIdNotEqualTo(id);
        List<Category> brotherCategorys = categoryMapper.selectByExample(categoryExample1);

        CategoryExample categoryExample2 = new CategoryExample();
        categoryExample2.createCriteria().andIdEqualTo(pid);
        List<Category> parentCategory = categoryMapper.selectByExample(categoryExample2);
        wxCategoryVo.setCurrentCategory(currentCategory);
        wxCategoryVo.setBrotherCategory(brotherCategorys);
        wxCategoryVo.setParentCategory(parentCategory);
        return wxCategoryVo;
    }

    @Override
    public WxgoodListVo getGoodsListByCategoryId(Integer categoryId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        WxgoodListVo wxgoodListVo = new WxgoodListVo();
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andIdNotEqualTo(categoryId);
        List<Category> filterCategoryList = categoryMapper.selectByExample(categoryExample);
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andCategoryIdEqualTo(categoryId);
        List<WxHomeIndexVoSimpleGoods> goodsList = goodsMapper.selectSimpleGoodsByExample(goodsExample);
        PageInfo<WxHomeIndexVoSimpleGoods> wxHomeIndexVoSimpleGoodsPageInfo = new PageInfo<>(goodsList);
        int size1 = wxHomeIndexVoSimpleGoodsPageInfo.getSize();
        wxgoodListVo.setFilterCategoryList(filterCategoryList);
        wxgoodListVo.setGoodsList(goodsList);
        wxgoodListVo.setCount(size1);
        return wxgoodListVo;
    }

    @Override
    public WxgoodListVo getGoodsListByBrandId(Integer brandId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andBrandIdEqualTo(brandId);
        List<WxHomeIndexVoSimpleGoods> goodsList = goodsMapper.selectSimpleGoodsByExample(goodsExample);
        PageInfo<WxHomeIndexVoSimpleGoods> wxHomeIndexVoSimpleGoodsPageInfo = new PageInfo<>(goodsList);
        int size1 = wxHomeIndexVoSimpleGoodsPageInfo.getSize();
        GoodsExample goodsExample1 = new GoodsExample();
        goodsExample1.createCriteria().andBrandIdEqualTo(brandId);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample1);
        if (goods.size() == 0)
            return null;
        List<Integer> integers = new ArrayList<>();
        for (Goods good : goods) {
            integers.add(good.getCategoryId());
        }

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andDeletedEqualTo(0).andIdIn(integers);
        List<Category> filterCategoryList = categoryMapper.selectByExample(categoryExample);

        WxgoodListVo wxgoodListVo = new WxgoodListVo();
        wxgoodListVo.setCount(size1);
        wxgoodListVo.setGoodsList(goodsList);
        wxgoodListVo.setFilterCategoryList(filterCategoryList);
        return wxgoodListVo;
    }

    @Override
    public WxgoodListVo getGoodsListByKeyword(String keyword, Integer page, Integer size, String sort, String order) {
        PageHelper.startPage(page, size);

        GoodsExample goodsExample = new GoodsExample();
        goodsExample.setOrderByClause(sort + " " + order);
        goodsExample.createCriteria().andNameLike("%" + keyword + "%");
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        if (goods.size() == 0)
            return null;
        List<Integer> categoryIds = new ArrayList<>();
        for (Goods good : goods) {
            Integer categoryId = good.getCategoryId();
            categoryIds.add(categoryId);
        }


        WxgoodListVo wxgoodListVo = new WxgoodListVo();
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andIdIn(categoryIds);
        List<Category> filterCategoryList = categoryMapper.selectByExample(categoryExample);


        List<WxHomeIndexVoSimpleGoods> goodsList = goodsMapper.selectSimpleGoodsByExample(goodsExample);
        PageInfo<WxHomeIndexVoSimpleGoods> wxHomeIndexVoSimpleGoodsPageInfo = new PageInfo<>(goodsList);
        int size1 = wxHomeIndexVoSimpleGoodsPageInfo.getSize();
        wxgoodListVo.setGoodsList(goodsList);
        wxgoodListVo.setFilterCategoryList(filterCategoryList);
        wxgoodListVo.setCount(size1);
        return wxgoodListVo;


    }

    @Autowired
    FootprintMapper footprintMapper;

    @Override
    public WxGoodsDetailVo getDetail(Integer goodsId) { //id是goodsId
        WxGoodsDetailVo wxGoodsDetailVo = new WxGoodsDetailVo();
        wxGoodsDetailVo.setUserHasCollect(0);
        Integer userId =  MyUtils.getUserId();
        //判断足迹里是否之前看过，之前看过则更新updatetime，没看过就新增
        if (MyUtils.getUserId() != null) {
            Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
            Footprint footprintExisted = footprintMapper.selectByUserIdAndGoodsId(userId,goodsId);
            if(footprintExisted == null){
                Footprint footprint = new Footprint();
                footprint.setId(null);
                footprint.setUserId(MyUtils.getUserId());
                footprint.setGoodsId(goods.getId());
                footprint.setAddTime(new Date());
                footprint.setUpdateTime(new Date());
                footprint.setDeleted(false);
                footprintMapper.insertSelective(footprint);
            }else {
                footprintExisted.setUpdateTime(new Date());
             footprintMapper.updateByPrimaryKey(footprintExisted);
            }
        }

        //userHasCollect
        if (userId != null) {
            CollectExample collectExample = new CollectExample();
            Integer zero = 0;
            Byte b = zero.byteValue();
            collectExample.createCriteria().andUserIdEqualTo(userId).andTypeEqualTo(b).andDeletedEqualTo(false);
            List<Collect> collectList = collectMapper.selectByExample(collectExample);
            for (Collect collect : collectList) {
                if (collect.getValueId() == goodsId) {
                    wxGoodsDetailVo.setUserHasCollect(1);
                }
            }
        }
        //shareImage
        //示例未实现

        //comment
        List<WxGoodsCommentVo> wxGoodsCommentVo = goodsCommentMapper.selectWxGoodsCommentVo(goodsId, userId);
        WxCommentBean wxCommentBean = new WxCommentBean();
        wxCommentBean.setCount(wxGoodsCommentVo.size());
        if (wxGoodsCommentVo.size() > 3) {
            List<WxGoodsCommentVo> tempWxGoodsCommentVoList = wxGoodsCommentVo.subList(0, 3);
            wxCommentBean.setData(tempWxGoodsCommentVoList);
        } else {
            wxCommentBean.setData(wxGoodsCommentVo);
        }

        wxGoodsDetailVo.setComment(wxCommentBean);

        //brand
        Integer brandId = goodsMapper.selectBrandIdById(goodsId);
        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        wxGoodsDetailVo.setBrand(brand);

        //info
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        wxGoodsDetailVo.setInfo(goods);

        //specificationList
        List<WxSpecificationListBean> wxSpecificationListBeanList = new ArrayList<WxSpecificationListBean>();
//        GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
//        goodsSpecificationExample.createCriteria().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
//        //此时select出来的specification是不同的，需要归类
//        List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);
        List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectByGoodsId(goodsId);
        List<String> nameExisted = new ArrayList<>();
        for (GoodsSpecification goodsSpecification : goodsSpecifications) {
            String name = goodsSpecification.getSpecification();
            if (!nameExisted.contains(name)) {
                nameExisted.add(name);
            }
        }
        for (String s : nameExisted) {
            GoodsSpecificationExample temp = new GoodsSpecificationExample();
            temp.createCriteria().andGoodsIdEqualTo(goodsId).andSpecificationEqualTo(s).andDeletedEqualTo(false);
            List<GoodsSpecification> specificationsTempList = goodsSpecificationMapper.selectByExample(temp);
            WxSpecificationListBean wxSpecificationListBean = new WxSpecificationListBean();
            wxSpecificationListBean.setName(s);
            wxSpecificationListBean.setValueList(specificationsTempList);
            wxSpecificationListBeanList.add(wxSpecificationListBean);
        }
        wxGoodsDetailVo.setSpecificationList(wxSpecificationListBeanList);

        //groupon = null

        //issue
        IssueExample issueExample = new IssueExample();
        issueExample.createCriteria().andDeletedEqualTo(0);
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        wxGoodsDetailVo.setIssue(issues);


        //attribute
        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        goodsAttributeExample.createCriteria().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        List<GoodsAttribute> goodsAttributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);
        wxGoodsDetailVo.setAttribute(goodsAttributes);

        //productList
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        goodsProductExample.createCriteria().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);
        wxGoodsDetailVo.setProductList(goodsProducts);

        //shareImage
        wxGoodsDetailVo.setShareImage("");
        //groupon
        wxGoodsDetailVo.setGroupon(new ArrayList<>());

        return wxGoodsDetailVo;
    }

    @Override
    public WxGoodsRelatedVo getRelated(Integer id) {
        WxGoodsRelatedVo wxGoodsRelatedVo = new WxGoodsRelatedVo();
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        Integer categoryId = goods.getCategoryId();
        List<WxGoodsListBean> goodsListBeans = goodsMapper.selectByCategoryId(categoryId);
        if (goodsListBeans.size() > 6) {
            goodsListBeans = goodsListBeans.subList(0, 6);
        }
        wxGoodsRelatedVo.setGoodsList(goodsListBeans);
        return wxGoodsRelatedVo;
    }
}
